package com.ssh.cp.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.ssh.cp.entity.FileEntity;
import com.ssh.cp.iservice.IFileService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class SearchAction {
	@Resource
	IFileService fileService;
	
	FileEntity fileEntity;
	
	private ActionContext ctx;
	
	
	private int draw ,recordsTotal, recordsFiltered,start,length;//用于返回给JQueryDatatables 数据参数
	
	private int libraryid,classid;
	
	
	JSONObject result;
	
	public String listFileByLibrary() {
		ctx=ActionContext.getContext();
		List<?> list = null;
		int userid = Integer.valueOf(ctx.getSession().get("userid").toString());
		recordsTotal = fileService.getCount(userid);
		
		System.out.println("=========libraryid:  "+libraryid);
//		if (libraryid==0) {
//			recordsFiltered = recordsTotal;
//			list = fileService.listFileByUserId(userid, start, length);
//		} else {
			list = fileService.listFileByLibrary(userid, libraryid, start, length);
			recordsFiltered = list.size();
//		}
			System.out.println("=========libraryid:"+list.size());
		result=new JSONObject();
		result.put("draw",draw);
		result.put("recordsTotal",recordsTotal);
		result.put("recordsFiltered",recordsFiltered);
		
		JSONArray array =JSONArray.fromObject(list);		
		result.put("data",array);
		
		return "success";
	}
	
	public String combinedQuery() {
		
		
		
		
		return "success";
	}
	
	
	
	public String listFileByClass() {
		ctx=ActionContext.getContext();
		List<?> list = null;
		int userid = Integer.valueOf(ctx.getSession().get("userid").toString());
		recordsTotal = fileService.getCount(userid);

		System.out.println("=========classid:"+classid);
		list = fileService.listFileByClass(userid, classid, start, length);
		recordsFiltered = list.size();
		
		
		result=new JSONObject();
		result.put("draw",draw);
		result.put("recordsTotal",recordsTotal);
		result.put("recordsFiltered",recordsFiltered);
		
		JSONArray array =JSONArray.fromObject(list);		
		result.put("data",array);
		
		return "success";
	}
	

	
	
	
	
	
	
	
	
	
	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}



	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getRecordsTotal() {
		return recordsTotal;
	}

	public void setRecordsTotal(int recordsTotal) {
		this.recordsTotal = recordsTotal;
	}

	public int getRecordsFiltered() {
		return recordsFiltered;
	}

	public void setRecordsFiltered(int recordsFiltered) {
		this.recordsFiltered = recordsFiltered;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getLibraryid() {
		return libraryid;
	}

	public void setLibraryid(int libraryid) {
		this.libraryid = libraryid;
	}

	public int getClassid() {
		return classid;
	}

	public void setClassid(int classid) {
		this.classid = classid;
	}
	
}
